package com.jhf.skypac_pda;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jhf.skypac_pda.base.BaseActivity;
import com.jhf.skypac_pda.model.DialogCallback;
import com.jhf.skypac_pda.model.LzyResponse;
import com.jhf.skypac_pda.model.loginModel;
import com.jhf.skypac_pda.utils.PrefUtils;
import com.jhf.skypac_pda.utils.Urls;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import static com.jhf.skypac_pda.utils.ConstantPara.JSON;

public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private Button sign_in_button;

    private EditText phonenum;

    private EditText password;

    private Context mContext;

    private ImageView login_maxim;

    private boolean isRemember = true;
    private boolean isUpdate = false;

    private TextView tv_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext=this;

        isUpdate = getIntent().getBooleanExtra("isUpdate", false);

        initView();
    }

    private void initView(){
        sign_in_button=findViewById(R.id.sign_in_button);
        sign_in_button.setOnClickListener(this);

        phonenum=findViewById(R.id.phonenum);
        phonenum.setText(PrefUtils.getString(LoginActivity.this, "userName", ""));
        password=findViewById(R.id.password);
        password.setText(PrefUtils.getString(LoginActivity.this, "password", ""));
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.sign_in_button:
                signIn();
              break;

          default:
         break;

        }
    }

    private void signIn(){

        String phnumber=phonenum.getText().toString();

        String pass=password.getText().toString();

        if(TextUtils.isEmpty(phnumber)){

            Toast.makeText(mContext,getResources().getString(R.string.error_empty_phonenum),Toast.LENGTH_SHORT).show();

            return ;
        }

        if(TextUtils.isEmpty(pass)){
            Toast.makeText(mContext,getResources().getString(R.string.error_empty_password),Toast.LENGTH_SHORT).show();

            return ;

        }

        Map<String ,Object> map=new HashMap<>();
        map.put("username",phnumber);
        map.put("password",pass);
        //map.put("app",ConstantPara.EXPRESS_CABINT);

        Gson gg=new Gson();
        String json =gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);
        OkGo.<LzyResponse<loginModel>>post(Urls.URL_LOGIN)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<loginModel>>(LoginActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<loginModel>> response) {
                        PrefUtils.setString(LoginActivity.this, "userName", phonenum.getText().toString().trim());
                        if (isRemember) {
                            PrefUtils.setString(LoginActivity.this, "password", password.getText().toString().trim());
                        } else {
                            PrefUtils.setString(LoginActivity.this, "password", "");
                        }

                        Log.e("LoginActivity","onSuccess--------------------");
                        loginModel loginCabModel = response.body().data;
                        //PrefUtils.setString(LoginActivity.this, "userId", loginCabModel.getId());
                        PrefUtils.setString(LoginActivity.this, "token", loginCabModel.getAccess_token());
                        PrefUtils.setString(LoginActivity.this, "expire", loginCabModel.getExpires_in());

                        if (isUpdate) {
                            finish();
                        } else {
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<loginModel>> response) {
                        Log.e("LoginActivity","onError---------------------");
                        Toast.makeText(getApplicationContext(), response.getException().getMessage(),Toast.LENGTH_SHORT).show();
                    }
                });



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Activity销毁时，取消网络请求
        OkGo.getInstance().cancelTag(this);


    }

    @Override
    protected void onResume() {
        super.onResume();
        phonenum.setText(PrefUtils.getString(LoginActivity.this, "userName", ""));
        password.setText(PrefUtils.getString(LoginActivity.this, "password", ""));
    }
}
